--Shared Functions Between All Server Scripts

function KKB.GetPlayerGroups( Ply )
	local UCL = Ply:GetUserGroup()
	local CustomUser = UCL
	local UniqueID = Ply:UniqueID()
	if ( KKB.CustomUsers[UniqueID] != nil ) then
		CustomUser = KKB.CustomUsers[UniqueID].Group
	end
	return UCL, CustomUser
end

--END Shared Functions

local function KKB_Check_AntiSpam( Ply )
	if ( Ply.KKB_AntiSpam_Time == nil ) then
		return false
	elseif ( Ply.KKB_AntiSpam_Time < CurTime() ) then
		return false
	end
	Ply:SendLua("GAMEMODE:AddNotify(\"Anti-Spam!\", NOTIFY_ERROR, 10)")
	return true
end

function KKB.SetOwner( Ent, Ply )
	if ( IsValid(Ply) and IsValid(Ent) ) then
		Ent.KKB_Owner = Ply
		Ent.KKB_OwnerID = Ply:UniqueID()
		Ent:SetNWString( "KKB_OwnerName", Ply:Nick() )
	end
end

if ( !CLEANUP_ADD ) then
	CLEANUP_ADD = cleanup.Add
end

function cleanup.Add(Player, Type, Entity)
	local Vars = { Player, Type, Entity }
	hook.Call( "CleanupAdd", GAMEMODE, unpack( Vars ) )
	
	CLEANUP_ADD( unpack( Vars ) )
end

local function KKBInitGM()
	function GAMEMODE:CleanupAdd(Player, Type, Entity)
		KKB.SetOwner( Entity, Player )
	end
end
hook.Add("Initialize", "KKBInitGM", KKBInitGM)




function KKB.PlayerSpawnProp( Ply, Model )
	local AntiSpam = KKB_Check_AntiSpam( Ply )
	if ( not AntiSpam ) then
		if ( table.HasValue( KKB.BlockedModels, Model ) ) then
			Ply:SendLua("GAMEMODE:AddNotify(\"Model Is Black-Listed!\", NOTIFY_ERROR, 10)")
			return false
		end
		if ( KKB.Settings.Server.SpawnTime > 0 ) then
			Ply.KKB_AntiSpam_Time = CurTime() + KKB.Settings.Server.SpawnTime
		end
	else
		--Anti-Spam
		return false
	end
end
hook.Add("PlayerSpawnProp", "KKB.PlayerSpawnProp", KKB.PlayerSpawnProp)

function KKB.PlayerSpawnVehicle( Ply, _, Name, _ )
	local AntiSpam = KKB_Check_AntiSpam( Ply )
	if ( not AntiSpam ) then
		local UCL, CustomUser = KKB.GetPlayerGroups( Ply )
		local Restriction = KKB.VehicleRestrictions[Name]
		local Blacklist = KKB.Settings.Vehicles.Blacklist
		local CanSpawn = true
		if ( Restriction != nil ) then
			local HasGroup = true
			if ( not table.HasValue(Restriction.Groups, UCL) ) then
				if ( not table.HasValue(Restriction.Groups, CustomUser ) ) then
					HasGroup = false
				end
			end
			if ( Blacklist and HasGroup ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"Vehicle Black-listed!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			elseif ( not Blacklist and not HasGroup ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"Vehicle Not White-listed!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			end
		else
			if ( not Blacklist ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"Vehicle Not On White-list!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			end
		end
		if ( not CanSpawn ) then
			if ( KKB.UserGroups[UCL].Spawn.Vehicles or KKB.UserGroups[CustomUser].Spawn.Vehicles ) then
				CanSpawn = true
			end
		end
		if ( not CanSpawn ) then
			return false
		end
		if ( KKB.Settings.Server.SpawnTime > 0 ) then
			Ply.KKB_AntiSpam_Time = CurTime() + KKB.Settings.Server.SpawnTime
		end
	else
		--Anti-Spam
		return false
	end
end
hook.Add("PlayerSpawnVehicle",  "KKB.PlayerSpawnVehicle", KKB.PlayerSpawnVehicle)

function KKB.PlayerSpawnNPC( Ply, NPC, _ )
	local AntiSpam = KKB_Check_AntiSpam( Ply )
	if ( not AntiSpam ) then
		local UCL, CustomUser = KKB.GetPlayerGroups( Ply )
		local Restriction = KKB.NPCRestrictions[Name]
		local Blacklist = KKB.Settings.NPCs.Blacklist
		local CanSpawn = true
		if ( Restriction != nil ) then
			local HasGroup = true
			if ( not table.HasValue(Restriction.Groups, UCL) ) then
				if ( not table.HasValue(Restriction.Groups, CustomUser ) ) then
					HasGroup = false
				end
			end
			if ( Blacklist and HasGroup ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"NPC Black-listed!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			elseif ( not Blacklist and not HasGroup ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"NPC Not White-listed!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			end
		else
			if ( not Blacklist ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"NPC Not On White-list!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			end
		end
		if ( not CanSpawn ) then
			if ( KKB.UserGroups[UCL].Spawn.NPCs or KKB.UserGroups[CustomUser].Spawn.NPCs ) then
				CanSpawn = true
			end
		end
		if ( not CanSpawn ) then
			return false
		end
		if ( KKB.Settings.Server.SpawnTime > 0 ) then
			Ply.KKB_AntiSpam_Time = CurTime() + KKB.Settings.Server.SpawnTime
		end
	else
		--Anti-Spam
		return false
	end
end
hook.Add("PlayerSpawnNPC", "KKB.PlayerSpawnNPC", KKB.PlayerSpawnNPC)

function KKB.PlayerSpawnSENT( Ply, Name )
	local AntiSpam = KKB_Check_AntiSpam( Ply )
	if ( not AntiSpam ) then
		local UCL, CustomUser = KKB.GetPlayerGroups( Ply )
		local Restriction = KKB.EntityRestrictions[Name]
		local Blacklist = KKB.Settings.Entitys.Blacklist
		local CanSpawn = true
		if ( Restriction != nil ) then
			local HasGroup = true
			if ( not table.HasValue(Restriction.Groups, UCL) ) then
				if ( not table.HasValue(Restriction.Groups, CustomUser ) ) then
					HasGroup = false
				end
			end
			if ( Blacklist and HasGroup ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"Sent Black-listed!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			elseif ( not Blacklist and not HasGroup ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"Sent Not White-listed!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			end
		else
			if ( not Blacklist ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"Sent Not On White-list!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			end
		end
		if ( not CanSpawn ) then
			if ( KKB.UserGroups[UCL].Spawn.Sents or KKB.UserGroups[CustomUser].Spawn.Sents ) then
				CanSpawn = true
			end
		end
		if ( not CanSpawn ) then
			return false
		end
		if ( KKB.Settings.Server.SpawnTime > 0 ) then
			Ply.KKB_AntiSpam_Time = CurTime() + KKB.Settings.Server.SpawnTime
		end
	else
		--Anti-Spam
		return false
	end
end
hook.Add("PlayerSpawnSENT", "KKB.PlayerSpawnSENT", KKB.PlayerSpawnSENT)

function KKB.PlayerSpawnSWEP( Ply, Class, _ )
	local AntiSpam = KKB_Check_AntiSpam( Ply )
	if ( not AntiSpam ) then
		local UCL, CustomUser = KKB.GetPlayerGroups( Ply )
		local Restriction = KKB.WeaponRestrictions[Name]
		local Blacklist = KKB.Settings.Weapons.Blacklist
		local CanSpawn = true
		if ( Restriction != nil ) then
			local HasGroup = true
			if ( not table.HasValue(Restriction.Groups, UCL) ) then
				if ( not table.HasValue(Restriction.Groups, CustomUser ) ) then
					HasGroup = false
				end
			end
			if ( Blacklist and HasGroup ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"Weapon Black-listed!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			elseif ( not Blacklist and not HasGroup ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"Weapon Not White-listed!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			end
		else
			if ( not Blacklist ) then
				Ply:SendLua("GAMEMODE:AddNotify(\"Weapon Not On White-list!\", NOTIFY_ERROR, 10)")
				CanSpawn = false
			end
		end
		if ( not CanSpawn ) then
			if ( KKB.UserGroups[UCL].Spawn.Weapons or KKB.UserGroups[CustomUser].Spawn.Weapons ) then
				CanSpawn = true
			end
		end
		if ( not CanSpawn ) then
			return false
		end
		if ( KKB.Settings.Server.SpawnTime > 0 ) then
			Ply.KKB_AntiSpam_Time = CurTime() + KKB.Settings.Server.SpawnTime
		end
	else
		--Anti-Spam
		return false
	end
end
hook.Add("PlayerSpawnSWEP", "KKB.PlayerSpawnSWEP", KKB.PlayerSpawnSWEP)
hook.Add("PlayerGiveSWEP", "KKB.PlayerGiveSWEP", KKB.PlayerSpawnSWEP)